package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.LockMassage;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class UserFreezeApiImpl implements UserFreezeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 插入用户冻结的数据
     */
    @Override
    public void save(LockMassage userFreeze) {
        userFreeze.setCreated(System.currentTimeMillis());
        mongoTemplate.save(userFreeze);
    }

    /**
     * 删除用户冻结数据
     */
    @Override
    public void delete(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        mongoTemplate.remove(query, LockMassage.class);
    }

    /**
     * 通过userId查询冻结数据
     */
    @Override
    public LockMassage findByUserId(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        LockMassage userFreeze = mongoTemplate.findOne(query, LockMassage.class);
        return userFreeze;
    }
}
